package test

import java.io.{BufferedWriter, File, FileWriter}
import java.nio.file.{Files, Paths}

import akka.actor.ActorSystem
import akka.stream.alpakka.csv.scaladsl.{CsvFormatting, CsvParsing, CsvToMap}
import akka.stream.javadsl.ZipWithN
import akka.stream.scaladsl.{FileIO, Flow, Framing, Keep, Sink, Source}
import akka.util.ByteString
import implicits.Implicits._
import org.apache.commons.io.FileUtils
import utils.Utils

import scala.collection.mutable.ArrayBuffer

/**
 * Created by yz on 15/7/2020
 */
object TestExec {

  def main(args: Array[String]): Unit = {

    implicit val system = ActorSystem("QuickStart")
    implicit val ec = system.dispatcher

    val startTime = System.currentTimeMillis()

    val parent = new File("C:\\rJunBase_database\\test")
    //    val file = new File(parent, "A1BG-AS1_LS005.csv")
    //    val file = new File(parent, "DLBC_expr2.csv")
    val file = new File(parent, "KIRC_expr2.csv")
    val headSink = Sink.head[String]
    val seqSink = Sink.seq[String]
    val foldSink = Sink.fold(List[String]())((x, y: String) => x ::: List(y))

    val outFile = new File(parent, "out.txt")

    val csvFrame = {
      CsvParsing.lineScanner(maximumLineLength = 1000 * 1000 * 1000).map(_.map(_.utf8String))
    }
    val lineFrame = Framing.delimiter(ByteString("\r\n"), maximumFrameLength = 1000 * 1000 * 1000,
      allowTruncation = true).map(_.utf8String)
    var count = 0
    val lines = FileIO.fromPath(Paths.get(file.getAbsolutePath)).via(csvFrame)

    val tmpDir = new File(parent, "tmpDir")
    tmpDir.listFiles().foreach(_.delete())

    //    val f = lines.grouped(20).zipWithIndex.runForeach { case (tmpColumns, i) =>
    //      count += 1
    //      Utils.logTime(count.toString, startTime)
    //      val newLines = tmpColumns.transpose.map(x => x.toList).toList
    //      newLines.toTxtFile(new File(tmpDir, s"${i}.txt"))
    //    }
    //
    //    f.foreach { x =>
    //      Utils.logTime(startTime)
    //      println("ok")
    //      val bw = new BufferedWriter(new FileWriter(outFile), 16384)
    //      val sources = tmpDir.listFiles().toList.map { file =>
    //        val s = FileIO.fromPath(Paths.get(file.getAbsolutePath)).via(lineFrame)
    //        s
    //      }
    //      val rs1 = Source.zipN(sources).runForeach { x =>
    //        val line = x.mkString("\t")
    //        bw.write(s"${line}\n")
    //      }
    //      rs1.foreach { x =>
    //        println("merge complete!")
    //        Utils.logTime(startTime)
    //        bw.close()
    //      }
    //    }


  }

}
